package com.microlearning.datasource.controller;

import com.microlearning.datasource.domain.User;
import com.microlearning.datasource.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @GetMapping("/get")
    public void getUser(int id){
        User user = userMapper.getUserById(id);
        System.out.println(user);
    }

    @PostMapping("/getAll")
    public void getUserList(){
        transactionTemplate.execute(status -> {
            try {
                List<User> userList = userMapper.getAllUsers();
                System.out.println(userList);
                return true;
            } catch (Exception e) {
                status.setRollbackOnly();
                throw new RuntimeException(e);
            }
        });
    }

}
